package com.fs.pandaFund.common.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
 * Author:HOU
 * date:2023/2/8  11:22
 * 案例:实名认证工具类
 **/

/*
 <!--实名认证-->
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.13.1</version>
        </dependency>
 */
public class RealNameUtil {
    public static String  RealNameAuthentication(String name,String id) throws IOException {
        String url = "https://eid.shumaidata.com/eid/check";
        String appCode = "3a669bd45bc344a6b6fe0474c58c8f77";

        Map<String, String> params = new HashMap<>();
        params.put("idcard", id);
        params.put("name", name);
        return postForm(appCode, url, params);
    }
    public static String postForm(String appCode, String url, Map<String, String> params) throws IOException {
        OkHttpClient client = new OkHttpClient.Builder().build();
        FormBody.Builder formbuilder = new FormBody.Builder();
        Iterator<String> it = params.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            formbuilder.add(key, params.get(key));
        }
        FormBody body = formbuilder.build();
        Request request = new Request.Builder().url(url).addHeader("Authorization", "APPCODE " + appCode).post(body).build();
        Response response = client.newCall(request).execute();
        System.out.println("返回状态码" + response.code() + ",message:" + response.message());
        String result = response.body().string();
        return result;
    }
}
